﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cs2102BookingSystem.Models;
using System.Globalization;

namespace Cs2102BookingSystem.Controllers
{
    public class BookingController : Controller
    {
        BookingEntities _db;

        public BookingController()
        {
            _db = new BookingEntities();
        }
        //
        // GET: /Booking/

        public ActionResult BookingUnEditableList()
        {
            DateTime current = System.DateTime.Now;
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE < current
                              && rm.ENDDATE < current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();
            return PartialView();
        }
        public ActionResult BookingUnEditableListUser(CLIENT user)
        {
            DateTime current = System.DateTime.Now;
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.EMAIL
                              && rm.STARTDATE < current
                              && rm.ENDDATE < current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();
            return PartialView();
        }
        public ActionResult BookingEditableList() 
        {
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE >= aWeek
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();
            return PartialView();
        }
        public ActionResult BookingEditableListUser(CLIENT user)
        {
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.EMAIL
                              && rm.STARTDATE >= aWeek
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();
            return PartialView();
        }
        public ActionResult BookingConfirmList() 
        {
            DateTime current = System.DateTime.Now;
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE <= aWeek
                              && rm.ENDDATE >= current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();
            
            return PartialView();
        }
        public ActionResult BookingConfirmListUser(CLIENT user)
        {
            DateTime current = System.DateTime.Now;
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.EMAIL
                              && rm.STARTDATE <= aWeek
                              && rm.ENDDATE >= current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).Take(10).ToList();

            return PartialView();
        }

        public ActionResult AllUnEditableBooking()
        {
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            DateTime current = System.DateTime.Now;
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE < current
                              && rm.ENDDATE < current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).ToList();
            return View();
        }
        public ActionResult AllEditableBooking()
        {
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE >= aWeek
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).ToList();
            return View();
        }
        public ActionResult AllConfirm()
        {
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            DateTime current = System.DateTime.Now;
            DateTime aWeek = System.DateTime.Now.AddDays(7);
            UserModel user = (UserModel)Session["User"];
            ViewData.Model = (from rm in _db.BOOKING
                              where rm.C_EMAIL == user.Email
                              && rm.STARTDATE <= aWeek
                              && rm.ENDDATE >= current
                              orderby rm.STARTDATE descending
                              orderby rm.ENDDATE
                              select rm).ToList();
            return View();
        }
        //
        // GET: /Booking/Details/5

        public ActionResult History()
        {
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            return View();
        }

        //
        // GET: /Booking/Create

        public ActionResult HotelBooking(BookingModel form)
        {
            try
            {
                if (!ChkValidUser())
                    return RedirectToAction("Index", "Home");
                var id = _db.BOOKING.Select(i=>i.BOOKINGID).Max()+1;
                UserModel user = (UserModel)Session["User"];
                BOOKING book = new BOOKING();
                book.C_EMAIL = user.Email;
                book.H_NAME = form.HotelName;
                book.H_BRANCHNAME = form.HotelBranch;
                book.H_COUNTRY = form.Country;
                book.NOOFROOM = 1;
                book.BOOKINGID = id;
                book.STARTDATE = System.DateTime.Now;
                book.ENDDATE = System.DateTime.Now.AddDays(1);
                var chkRmExist = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == form.Room).Count();
                if (chkRmExist > 0) {
                    ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == form.Room).First();
                    if (rm.NOOFROOM < 1) {
                        return RedirectToAction("Error", "Home");
                    }
                }
                var roomType = new SelectList(from rm in _db.ROOM
                                              where rm.H_NAME == form.HotelName
                                              && rm.H_BRANCHNAME == form.HotelBranch
                                              && rm.H_COUNTRY == form.Country
                                              select new SelectListItem
                                              {
                                                  Text = rm.ROOMTYPE,
                                                  Value = rm.ROOMTYPE
                                              }, "Value", "Text", form.Room);
                SelectList bedType;
                if (!string.IsNullOrEmpty(form.Room))
                {
                    bedType = new SelectList(from rm in _db.ROOM
                                             where rm.H_NAME == book.H_NAME
                                             && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                             && rm.H_COUNTRY == book.H_COUNTRY
                                             && rm.ROOMTYPE == book.R_ROOMTYPE
                                             select new SelectListItem
                                             {
                                                 Text = rm.BEDTYPE,
                                                 Value = rm.BEDTYPE
                                             }, "Value", "Text");
                    book.R_ROOMTYPE = form.Room;
                }
                else
                {
                    bedType = new SelectList("");
                }
                ViewData["room"] = roomType;
                ViewData["bed"] = bedType;
                return View(book);
            }
            catch {
                return RedirectToAction("Error", "Home");
            }
        }
  
          
        // POST: /Booking/Create
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HotelBooking(FormCollection form) {
            try
            {
                if (!ChkValidUser())
                    return RedirectToAction("Index", "Home");
                if (string.IsNullOrEmpty(form["submit1"]))
                {
                
                BOOKING book = new BOOKING();
                UserModel user = (UserModel)Session["User"];
                var startDate = form.Get("STARTDATE");
                book.STARTDATE = DateTime.ParseExact(startDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var endDate = form.Get("ENDDATE");
                book.ENDDATE = DateTime.ParseExact(endDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                TryUpdateModel(book, new string[] { "C_EMAIL", "H_NAME", "H_BRANCHNAME", "H_COUNTRY", "R_ROOMTYPE", "BOOKINGID", "R_BEDTYPE", "NOOFROOM", "PRICEPERROOM" }, form.ToValueProvider());
                var chk = _db.BOOKING.Where(i => i.BOOKINGID == book.BOOKINGID).Count();
                book.C_EMAIL = user.Email;
                if (chk > 0) {
                    return RedirectToAction("Error", "Home");
                }
                var roomType = new SelectList(from rm in _db.ROOM
                                              where rm.H_NAME == book.H_NAME
                                              && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                              && rm.H_COUNTRY == book.H_COUNTRY
                                              select new SelectListItem
                                              {
                                                  Text = rm.ROOMTYPE,
                                                  Value = rm.ROOMTYPE
                                              }, "Value", "Text", book.R_ROOMTYPE);
                SelectList bedType = new SelectList(from rm in _db.ROOM
                                             where rm.H_NAME == book.H_NAME
                                             && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                             && rm.H_COUNTRY == book.H_COUNTRY
                                             && rm.ROOMTYPE == book.R_ROOMTYPE
                                             select new SelectListItem
                                             {
                                                 Text = rm.BEDTYPE,
                                                 Value = rm.BEDTYPE
                                             }, "Value", "Text");
                var chk1 = (from rm in _db.ROOM
                           where rm.H_NAME == book.H_NAME
                           && rm.H_BRANCHNAME == book.H_BRANCHNAME
                           && rm.H_COUNTRY == book.H_COUNTRY
                           && rm.ROOMTYPE == book.R_ROOMTYPE
                           && rm.BEDTYPE == book.R_BEDTYPE
                           select rm).Count();
                Boolean chk2 = false;
                if ((book.STARTDATE > book.ENDDATE))
                {
                    ModelState.AddModelError("STARTDATE", "Arrival Date cannot be after departure date");
                    chk2 = true;
                }
                int numDaysDiff = Math.Abs(book.STARTDATE.Subtract(book.ENDDATE).Days);
                if (numDaysDiff <= 0)
                {
                    ModelState.AddModelError("ENDDATE", "Cannot book a room that departure today");
                    chk2 = true;
                }
                if (!string.IsNullOrEmpty(book.R_BEDTYPE) && book.NOOFROOM != 0 && (chk1 > 0) && chk2 == false)
                {
                    book.PRICEPERROOM = decimal.Parse((from rm in _db.ROOM
                                                       where rm.H_NAME == book.H_NAME
                                                       && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                       && rm.H_COUNTRY == book.H_COUNTRY
                                                       && rm.ROOMTYPE == book.R_ROOMTYPE
                                                       && rm.BEDTYPE == book.R_BEDTYPE
                                                       select rm.PRICE).First().ToString());
                }
                if (book.NOOFROOM == 0)
                {
                    ModelState.AddModelError("NOOFROOM", "Require to book at least 1 room");
                }
                if (string.IsNullOrEmpty(book.R_BEDTYPE))
                {
                    ModelState.AddModelError("R_BEDTYPE", "Require to select a room type");
                }
                var chkRmExist = _db.ROOM.Where(m => m.H_NAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).Count();
                if (chkRmExist > 0)
                {
                    ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                    if (rm.NOOFROOM < book.NOOFROOM)
                    {
                        ModelState.AddModelError("NOOFROOM", "There is insufficient room available currently");
                    }
                }
                if (book.NOOFROOM <= 0)
                {
                    ModelState.AddModelError("NOOFROOM", "Please re-enter the no of room");
                }

                ViewData["room"] = roomType;
                ViewData["bed"] = bedType;
                return View(book);

                }
                else
                {

                    UserModel user = (UserModel)Session["User"];
                    BOOKING book = new BOOKING();
                    book.C_EMAIL = user.Email;
                    var startDate = form.Get("STARTDATE");
                    book.STARTDATE = DateTime.ParseExact(startDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    var endDate = form.Get("ENDDATE");
                    book.ENDDATE = DateTime.ParseExact(endDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    TryUpdateModel(book, new string[] { "C_EMAIL", "H_NAME", "H_BRANCHNAME", "H_COUNTRY", "R_ROOMTYPE", "BOOKINGID", "R_BEDTYPE", "NOOFROOM", "PRICEPERROOM" }, form.ToValueProvider());
                    Boolean chk2 = false;
                    if ((book.STARTDATE > book.ENDDATE))
                    {
                        ModelState.AddModelError("STARTDATE", "Arrival Date cannot be after departure date");
                        chk2 = true;
                    }
                    int numDaysDiff = Math.Abs(book.STARTDATE.Subtract(book.ENDDATE).Days);
                    if (numDaysDiff <= 0)
                    {
                        ModelState.AddModelError("ENDDATE", "Cannot book a room that departure today");
                        chk2 = true;
                    }
                    var chkRmExist = _db.ROOM.Where(m => m.H_NAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).Count();
                    if (chkRmExist > 0)
                    {
                        ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                        if (rm.NOOFROOM < book.NOOFROOM)
                        {
                            ModelState.AddModelError("NOOFROOM", "There is insufficient room available currently");
                            chk2 = true;
                        }
                    }
                    if (book.NOOFROOM <= 0)
                    {
                        ModelState.AddModelError("NOOFROOM", "Please re-enter the no of room");
                        chk2 = true;
                    }
                    if (!string.IsNullOrEmpty(book.BOOKINGID.ToString()) && !string.IsNullOrEmpty(book.H_NAME) && !string.IsNullOrEmpty(book.H_COUNTRY) && !string.IsNullOrEmpty(book.H_BRANCHNAME) && chk2 == false && !string.IsNullOrEmpty(book.R_BEDTYPE) && !string.IsNullOrEmpty(book.R_ROOMTYPE) && (book.NOOFROOM > 0) && chk2 == false)
                    {
                        _db.AddToBOOKING(book);
                        ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                        rm.NOOFROOM = rm.NOOFROOM - book.NOOFROOM;
                        _db.SaveChanges();
                    }
                    else {
                        
                        if (book.NOOFROOM == 0)
                        {
                            ModelState.AddModelError("NOOFROOM", "Require to book at least 1 room");
                        }
                        if (string.IsNullOrEmpty(book.R_ROOMTYPE))
                        {
                            ModelState.AddModelError("R_ROOMTYPE", "Require to select a room type");
                        }                       
                        if (string.IsNullOrEmpty(book.R_BEDTYPE))
                        {
                            ModelState.AddModelError("R_BEDTYPE", "Require to select a Bed type");
                        }
                        var roomType = new SelectList(from rm in _db.ROOM
                                                      where rm.H_NAME == book.H_NAME
                                                      && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                      && rm.H_COUNTRY == book.H_COUNTRY
                                                      select new SelectListItem
                                                      {
                                                          Text = rm.ROOMTYPE,
                                                          Value = rm.ROOMTYPE
                                                      }, "Value", "Text", book.R_ROOMTYPE);
                        SelectList bedType = new SelectList(from rm in _db.ROOM
                                                            where rm.H_NAME == book.H_NAME
                                                            && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                            && rm.H_COUNTRY == book.H_COUNTRY
                                                            && rm.ROOMTYPE == book.R_ROOMTYPE
                                                            select new SelectListItem
                                                            {
                                                                Text = rm.BEDTYPE,
                                                                Value = rm.BEDTYPE
                                                            }, "Value", "Text");
                        ViewData["room"] = roomType;
                        ViewData["bed"] = bedType;
                        return View(book);
                    }
                    return RedirectToAction("Index", "Home");
                }
            }
            catch
            {
                return RedirectToAction("Error", "Home");
            }
        }

        //
        // GET: /Booking/Delete/5

        public ActionResult DeleteReservation(decimal id)
        {
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            var countChk = _db.BOOKING.Where(i => i.BOOKINGID == id).Count();
            if (countChk > 0)
            {
                var transaction = _db.BOOKING.First(m => m.BOOKINGID == id);
                _db.BOOKING.DeleteObject(transaction);
                _db.SaveChanges();
                return RedirectToAction("History");
            }
            return RedirectToAction("Error", "Home");
        }
        public ActionResult EditBooking(decimal id)
        {
            // Get movie to update
            var transaction = _db.BOOKING.First(m => m.BOOKINGID == id);
            var roomType = new SelectList(from rm in _db.ROOM
                                          where rm.H_NAME == transaction.H_NAME
                                          && rm.H_BRANCHNAME == transaction.H_BRANCHNAME
                                          && rm.H_COUNTRY == transaction.H_COUNTRY
                                          select new SelectListItem
                                          {
                                              Text = rm.ROOMTYPE,
                                              Value = rm.ROOMTYPE
                                          }, "Value", "Text", transaction.R_ROOMTYPE);
            SelectList bedType = new SelectList(from rm in _db.ROOM
                                                where rm.H_NAME == transaction.H_NAME
                                                && rm.H_BRANCHNAME == transaction.H_BRANCHNAME
                                                && rm.H_COUNTRY == transaction.H_COUNTRY
                                                && rm.ROOMTYPE == transaction.R_ROOMTYPE
                                                select new SelectListItem
                                                {
                                                    Text = rm.BEDTYPE,
                                                    Value = rm.BEDTYPE
                                                }, "Value", "Text", transaction.R_BEDTYPE);
            ViewData.Model = transaction;
            ViewData["room"] = roomType;
            ViewData["bed"] = bedType;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditBooking(FormCollection form)
        {
            try
            {
                if (!ChkValidUser())
                    return RedirectToAction("Index", "Home");
                if (string.IsNullOrEmpty(form["submit1"]))
                {
                    var raw = form["BOOKINGID"];
                    string[] raw1 = raw.Split(',');
                    var id = decimal.Parse(raw1[0]);
                    var book = _db.BOOKING.First(m => m.BOOKINGID == id);
                    var startDate = form.Get("STARTDATE");
                    book.STARTDATE = DateTime.ParseExact(startDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    var endDate = form.Get("ENDDATE");
                    book.ENDDATE = DateTime.ParseExact(endDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    TryUpdateModel(book, new string[] { "C_EMAIL", "H_NAME", "H_BRANCHNAME", "H_COUNTRY", "R_ROOMTYPE", "BOOKINGID", "R_BEDTYPE", "NOOFROOM", "PRICEPERROOM" }, form.ToValueProvider());
                    var roomType = new SelectList(from rm in _db.ROOM
                                                  where rm.H_NAME == book.H_NAME
                                                  && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                  && rm.H_COUNTRY == book.H_COUNTRY
                                                  select new SelectListItem
                                                  {
                                                      Text = rm.ROOMTYPE,
                                                      Value = rm.ROOMTYPE
                                                  }, "Value", "Text", book.R_ROOMTYPE);
                    SelectList bedType = new SelectList(from rm in _db.ROOM
                                                        where rm.H_NAME == book.H_NAME
                                                        && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                        && rm.H_COUNTRY == book.H_COUNTRY
                                                        && rm.ROOMTYPE == book.R_ROOMTYPE
                                                        select new SelectListItem
                                                        {
                                                            Text = rm.BEDTYPE,
                                                            Value = rm.BEDTYPE
                                                        }, "Value", "Text", book.R_BEDTYPE);
                    var chk = (from rm in _db.ROOM
                               where rm.H_NAME == book.H_NAME
                               && rm.H_BRANCHNAME == book.H_BRANCHNAME
                               && rm.H_COUNTRY == book.H_COUNTRY
                               && rm.ROOMTYPE == book.R_ROOMTYPE
                               && rm.BEDTYPE == book.R_BEDTYPE
                               select rm).Count();
                    Boolean chk2 = false;
                    if ((book.STARTDATE > book.ENDDATE))
                    {
                        ModelState.AddModelError("STARTDATE", "Arrival Date cannot be after departure date");
                        chk2 = true;
                    }
                    int numDaysDiff = Math.Abs(book.STARTDATE.Subtract(book.ENDDATE).Days);
                    if (numDaysDiff <= 0)
                    {
                        ModelState.AddModelError("ENDDATE", "Cannot book a room that departure today");
                        chk2 = true;
                    }
                    var chkRmExist = _db.ROOM.Where(m => m.H_NAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).Count();
                    if (chkRmExist > 0)
                    {
                        ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                        if (rm.NOOFROOM < book.NOOFROOM)
                        {
                            ModelState.AddModelError("NOOFROOM", "There is insufficient room available currently");
                            chk2 = true;
                        }
                    }
                    if (book.NOOFROOM <= 0)
                    {
                        ModelState.AddModelError("NOOFROOM", "Please re-enter the no of room");
                        chk2 = true;
                    }
                    if (chk > 0 && chk2 == false)
                    {
                        book.PRICEPERROOM = decimal.Parse((from rm in _db.ROOM
                                                           where rm.H_NAME == book.H_NAME
                                                           && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                           && rm.H_COUNTRY == book.H_COUNTRY
                                                           && rm.ROOMTYPE == book.R_ROOMTYPE
                                                           && rm.BEDTYPE == book.R_BEDTYPE
                                                           select rm.PRICE).First().ToString());
                    }
                    else {
                        ModelState.AddModelError("R_BEDTYPE", "Require to select a room type");
                        book.PRICEPERROOM = 0;
                    }
                    if (book.NOOFROOM == 0)
                    {
                        ModelState.AddModelError("NOOFROOM", "Require to book at least 1 room");
                    }
                    if (string.IsNullOrEmpty(book.R_BEDTYPE))
                    {
                        ModelState.AddModelError("R_BEDTYPE", "Require to select a room type");
                    }
                    ViewData["room"] = roomType;
                    ViewData["bed"] = bedType;
                    return View(book);

                }
                else
                {

                    var raw = form["BOOKINGID"];
                    string[] raw1 = raw.Split(',');
                    var id = decimal.Parse(raw1[0]);
                    var book = _db.BOOKING.First(m => m.BOOKINGID == id);
                    var startDate = form.Get("STARTDATE");
                    book.STARTDATE = DateTime.ParseExact(startDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    var endDate = form.Get("ENDDATE");
                    book.ENDDATE = DateTime.ParseExact(endDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    TryUpdateModel(book, new string[] { "C_EMAIL", "H_NAME", "H_BRANCHNAME", "H_COUNTRY", "R_ROOMTYPE", "BOOKINGID", "R_BEDTYPE", "NOOFROOM", "PRICEPERROOM" }, form.ToValueProvider());

                    Boolean chk2 = false;
                    if ((book.STARTDATE > book.ENDDATE))
                    {
                        ModelState.AddModelError("STARTDATE", "Arrival Date cannot be after departure date");
                        chk2 = true;
                    }
                    int numDaysDiff = Math.Abs(book.STARTDATE.Subtract(book.ENDDATE).Days);
                    if (numDaysDiff <= 0)
                    {
                        ModelState.AddModelError("ENDDATE", "Cannot book a room that departure today");
                        chk2 = true;
                    }
                    var chkRmExist = _db.ROOM.Where(m => m.H_NAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).Count();
                    if (chkRmExist > 0)
                    {
                        ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                        if (rm.NOOFROOM < book.NOOFROOM)
                        {
                            ModelState.AddModelError("NOOFROOM", "There is insufficient room available currently");
                            chk2 = true;
                        }
                    }
                    if (book.NOOFROOM <= 0)
                    {
                        ModelState.AddModelError("NOOFROOM", "Please re-enter the no of room");
                        chk2 = true;
                    }
                    if (!string.IsNullOrEmpty(book.BOOKINGID.ToString()) && !string.IsNullOrEmpty(book.H_NAME) && !string.IsNullOrEmpty(book.H_COUNTRY) && !string.IsNullOrEmpty(book.H_BRANCHNAME) && chk2 == false && !string.IsNullOrEmpty(book.R_BEDTYPE) && !string.IsNullOrEmpty(book.R_ROOMTYPE) && (book.NOOFROOM > 0) )
                    {
                        ROOM rm = _db.ROOM.Where(m => m.H_BRANCHNAME == book.H_NAME && m.H_BRANCHNAME == book.H_BRANCHNAME && m.H_COUNTRY == book.H_COUNTRY && m.ROOMTYPE == book.R_ROOMTYPE).First();
                        rm.NOOFROOM = rm.NOOFROOM - book.NOOFROOM ;
                        _db.SaveChanges();
                    }
                    else
                    {

                        if (book.NOOFROOM == 0)
                        {
                            ModelState.AddModelError("NOOFROOM", "Require to book at least 1 room");
                        }
                        if (string.IsNullOrEmpty(book.R_ROOMTYPE))
                        {
                            ModelState.AddModelError("R_ROOMTYPE", "Require to select a room type");
                        }
                        if (string.IsNullOrEmpty(book.R_BEDTYPE))
                        {
                            ModelState.AddModelError("R_BEDTYPE", "Require to select a Bed type");
                        }
                        var roomType = new SelectList(from rm in _db.ROOM
                                                      where rm.H_NAME == book.H_NAME
                                                      && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                      && rm.H_COUNTRY == book.H_COUNTRY
                                                      select new SelectListItem
                                                      {
                                                          Text = rm.ROOMTYPE,
                                                          Value = rm.ROOMTYPE
                                                      }, "Value", "Text", book.R_ROOMTYPE);
                        SelectList bedType = new SelectList(from rm in _db.ROOM
                                                            where rm.H_NAME == book.H_NAME
                                                            && rm.H_BRANCHNAME == book.H_BRANCHNAME
                                                            && rm.H_COUNTRY == book.H_COUNTRY
                                                            && rm.ROOMTYPE == book.R_ROOMTYPE
                                                            select new SelectListItem
                                                            {
                                                                Text = rm.BEDTYPE,
                                                                Value = rm.BEDTYPE
                                                            }, "Value", "Text");
                        ViewData["room"] = roomType;
                        ViewData["bed"] = bedType;
                        return View(book);
                    }
                    return RedirectToAction("History");
                }
            }
            catch
            {
                return RedirectToAction("Error", "Home");
            }
        }

        public ActionResult BookingDetail(decimal id){
            // Get movie to update
            if (!ChkValidUser())
                return RedirectToAction("Index", "Home");
            var transaction = _db.BOOKING.First(m => m.BOOKINGID == id);

            ViewData.Model = transaction;
            return View();
        }

        public ActionResult HotelImage(HOTEL hotel)
        {
            var chk = (from h1 in _db.HOTEL
                       where h1.HOTEL_NAME == hotel.HOTEL_NAME
                       && h1.BRANCHNAME == hotel.BRANCHNAME
                       && h1.COUNTRY == hotel.COUNTRY
                       select h1.HOTELIMAGE).Count();
            if (chk > 0)
            {
                var image = (from h1 in _db.HOTEL
                             where h1.HOTEL_NAME == hotel.HOTEL_NAME
                             && h1.BRANCHNAME == hotel.BRANCHNAME
                             && h1.COUNTRY == hotel.COUNTRY
                             select h1.HOTELIMAGE).First();//_db.BOOKING.First(m => m.BOOKINGID == id);
                hotel.HOTELIMAGE = image;
            }
            return PartialView(hotel);

        }

        private Boolean ChkValidUser(){

            UserModel user = (UserModel)Session["User"];
            if(string.IsNullOrEmpty(user.Email))
                return false;
            return true;
        }
    }
}
